<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Url;
use think\Config;
use think\Request;
use think\helper\Time;
use think\Loader;

class News extends Common{

	public function _initialize(){
		if(request()->isGet()){

			$action= strtolower ( Request::instance()->action() );
			$controller=strtolower( Request::instance()->controller());

			switch ($action) {
				
				case 'newslist':
					$this->assign('title',"新闻列表");
					break;
				case 'newsinfo':
					$this->assign('title',"新闻详情");
					break;
			}

			$this->assign('controller',$controller);
		}
	}
	
	

	#删除分类#
	public function videodel(){
		$videoid=input('videoid');
		echo Db::name('video_list')->where(['videoid'=>$videoid])->delete();
	}

	#分类分类管理#
	
	#分类列表#
	public function newslist(){
		if(request()->isGet()){
			if(request()->isajax()){

				//接口
				$cateid=input('cateid');
				$page=input('page');
				$limit=input('limit',30);
				$pid=input('pid');
				$keyword=input('keyword');
				$start=input('start');
				$end=input('end');

				$map=[];

				if($cateid){
					$map['news.cateid']=$cateid;
				}
				if($keyword){
					$map['news.title|news.keyword|news.description|news.content|cate.catename']= ['like',"%$keyword%"];
				}
				if($start){
					$map['news.addtime']=array('egt',$start);
				}
				if($end){
					$map['news.addtime']=array('elt',$end);
				}
				if($start&&$end){
					$map['news.addtime']=array('between',[$start,$end]);
				}
				//print_r($map);exit;

				$list=Db::name('news_list')->alias('news')->join(config('database.prefix').'cate_list cate','news.cateid=cate.cateid' )->where($map)->order('news.newsid desc')->field('news.*,cate.catename')->page($page.','.$limit)->select();
				
				Url::root("/index.php");
				foreach ($list as $key => $v) {

					$list[$key]['thumb']=adminthumb($v['thumb']);

					if($v['status']=='1'){
						$list[$key]['status']="<span class='layui-badge-rim layui-bg-green'>正常</span>";
					}else{
						$list[$key]['status']="<span class='layui-badge-rim layui-bg-gray'>下架</span>"; 
					}

					$list[$key]['title']="<a href='".url('index/index/content',array('cateid'=>$v['cateid'],'newsid'=>$v['newsid']),'html',true)."' target='_blank'>".$v['title']."</a>";
					
				}

				$arr=array(
					'code'=>"0",
					'count'=>Db::name('news_list')->alias('news')->join(config('database.prefix').'cate_list cate','news.cateid=cate.cateid' )->where($map)->count(),
					'data'=>$list
				);
				return json($arr);
			}
			
			$this->assign("catelist",action('Cate/getcatelistdata'));
			return $this->fetch();
		}
	} 

	
	#分类分类#
	public function newsinfo(){
		if(request()->isGet()){

			$newsid=input('newsid');
			if(Db::name('cate_list')->where("status","1")->count("cateid")=='0'){
				$this->error("请先添加一个分类",url('cate/cateinfo'));
			}
			//$newslist=Db::name('news_list')->where('pid','0')->order('newspx desc')->field('newsid,pid,newsname')->select();

			$catelist=action('Cate/getcatelistdata');
	
			if($newsid){

				$info=Db::name('news_list')->where(['newsid'=>$newsid])->find();
				if(empty($info)){
					$this->error("当前新闻不存在~");
				}
				$info['photos']=json_decode($info['photos'],true);
				$info['files']=json_decode($info['files'],true);
				$this->assign('info',$info);
			}
			
			//目录下文件,暂时取消
			/*$dir="./app/index/view/index/";
			$file_list=scandir($dir);
			foreach ($file_list as $k => $v) {
				if(){

				}
			}*/
			$this->assign('pages',$this->gettemplate());
			$this->assign('catelist',$catelist);
			$this->assign('newslist',$newslist);
			return $this->fetch();
		}else{

			$data=input('post.');
		//转json
			$data['photos']=json_encode($data['photos']);

			$data['status']=empty($data['status'])?"0":$data['status'];
			$data['files']=json_encode($data['files']);
			unset($data['file']);

			if($data['newsid']){

				$map=array(
					'newsid'=>array('neq',$data['newsid']),
					'newsname'=>$data['newsname']
				);
			
				/*if(Db::name('news_list')->where($map)->count()>0){
					$this->error("当前分类已经存在!");
				}*/

				if($data['url'] ==''){
					$data['url']=config('site.rukou')."/content-{$data['newsid']}.html";
				}

				$data['uptime']=date('Y-m-d H:i:s',time());

				$r=Db::name('news_list')->update($data);
				
				$url=Url('news/newslist');

				if($r==true){
					$this->success("新闻修改成功！",$url);
				}else{
					$this->error("新闻修改失败！",$url);
				}

			}else{
				
				/*if(Db::name('news_list')->where(['newsname'=>$data['newsname']])->count()>0){
					$this->error("当前分类已经存在!");
				}*/

				$newsid=Db::name('news_list')->insertGetId($data);
				if($newsid){
					if($data['url'] ==''){
						$url=config('site.rukou')."/content-{$newsid}.html";
					}
					Db::name('news_list')->where(['newsid'=>$newsid])->update(['url'=>$url]);
					$this->success("新闻添加成功~",url('newslist'));
				}else{
					$this->error("添加失败，请重试~");
				}


			}
		}
	}

	#删除新闻#
	public function newsdel(){
		$newsid=input('newsid');

		if(config('site.file_del')=='1'){
			$newsinfo=Db::name('news_list')->where(['newsid'=>$newsid])->find();
			//删除缩略图
			$this->removefile($newsinfo['thumb']);
			//删除图集
			$photos=json_decode($newsinfo['photos'],true);
			if(is_array($photos['path'])){
				foreach ($photos['path'] as $k => $v) {
					$this->removefile($v);
				}
			}
			//删除文件
			if(isset($newsinfo['files'])){
				$files=json_decode($newsinfo['files'],true);
				$this->removefile($files['src']);
			}
		}

		echo Db::name('news_list')->where(['newsid'=>$newsid])->delete();
		
	}

	public function content(){
		if(request()->isGet()){
			$newsid=input('newsid');
			$newsid=input('newsid');

			$this->assign('title',"文章详情");
			
			if($newsid){
				$map=array(
					'newsid'=>$newsid,
					'newsid'=>$newsid
				);
			}else{
				$map=array(
					'newsid'=>$newsid
				);
			}
			$newstype=Db::name('news_list')->where('newsid',$newsid)->value('newstype');

			if($newstype=='文章模型'&& $newsid==''){
				return $this->fetch('content');
			}
			
			//print_r($newstype);exit;
			

			$info=Db::name('news_list')->where($map)->find();
			
			if($info){
				$this->assign('info',$info);
			}
			
			return $this->fetch('content');
			exit;

			switch ($newstype) {

				case '单页面':
					

					$info=Db::name('news_list')->where('newsid',$newsid)->find();
					$this->assign('info',$info);
					return $this->fetch('content');
					
					break;

				case '文章模型':
					
					$this->redirect('newslist',array('newsid'=>$newsid));
					exit;

					$list=Db::name('news_list')->where('newsid',$newsid)->order('addtime desc')->select();
					$this->assign('info',$info);
					return $this->fetch('newslist');
				break;
				default:
					# code...
					break;
			}

		}else{


			$data=input('post.');
			unset($data['file']);

			//修改
			if($data['newsid']){

				$r=Db::name('news_list')->update($data);
				
				if($r==true){
					$this->success("修改成功~",url('newslist',array('newsid'=>$data['newsid'])));
				}else{
					$this->error("修改失败~",url('newslist',array('newsid'=>$data['newsid'])));
				}

			//添加
			}else{

				$r=Db::name('news_list')->insert($data);

				if($r==true){
					$this->success("添加成功~",url('newslist',array('newsid'=>$data['newsid'])));
				}else{
					$this->error("添加失败~",url('newslist',array('newsid'=>$data['newsid'])));
				}


			}



		}
	}
	public function getpageinfo(){
		if(request()->isajax()){
			$cateid=input('cateid');
			$template=Db::name('cate_list')->where('cateid',$cateid)->value('template');
			if(strstr($template,"/")){
				$data=explode("/",$template);
				echo $data[1];
			}else{
				echo $template;
			}
		}
	}




}